--- 战策十·偷梁换柱
local policy10 = fk.CreateSkill {
    name = "replace_something",
    tags = { Skill.Compulsory },
    dynamic_name = function (self, player, lang)
      return player == Self and self.name or "unknownpolicy"
    end,
    dynamic_desc = function (self, player, lang)
      return player == Self and self.name or "unknownpolicy"
    end,
}

local HG = require "packages.new_heg_diy_my.util"

Fk:loadTranslationTable {
    ["@[replace_something_mark]"] = "偷梁换柱",
    ["replace_something_ask"] = "战策-偷梁换柱：你可以观看当前角色所有手牌并用一张手牌交换其中一张",
    ["$replace_something-to"] = "对方手牌",
    ["$replace_something-my"] = "你的手牌",
    ["#replace_something-card"] = "偷梁换柱：选择两张牌交换",
    ["#replace_something-method"] = "偷梁换柱",
}

Fk:addPoxiMethod{
    name = "#replace_something-method",
    prompt = "#replace_something-card",
    card_filter = function(to_select, selected, data)
      if data and #selected < 3 then
        if table.contains(data[1][2], to_select) then
          local n = 0
          for _, id in ipairs(selected) do
            if table.contains(data[1][2], id) then
              n = n + 1
            end
          end
          return n < 1
        else
          local n = 0
          for _, id in ipairs(selected) do
            if table.contains(data[2][2], id) then
              n = n + 1
            end
          end
          return n < 1
        end
      end
    end,
    feasible = function (selected, data, extra_data)
      return #selected == 2
    end,
  }

policy10:addEffect(fk.EventPhaseStart, {
    anim_type = "control",
    can_trigger = function(self, event, target, player, data)
        local mark_name = "@[private]" .. policy10.name .. "_mark"
        return player:getMark(mark_name) ~= 0 and target.phase == Player.Play and target ~= player and
        player:distanceTo(target) == 1 and not target:isKongcheng() and not player:isKongcheng()
    end,
    on_cost = function(self, event, target, player, data)
        return player.room:askToSkillInvoke(player, { skill_name = "unknownpolicy", prompt = "replace_something_ask" })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local mark_name = "@[private]" .. policy10.name .. "_mark"
        local cards = target:getCardIds("h")
        local handcards = player:getCardIds("h")
        cards = room:askToPoxi(player, {
            poxi_type = "#replace_something-method",
            data = {
            { "$replace_something-to", cards },
            { "$replace_something-my", handcards }

        }})
        if #cards > 0 then
          handcards = table.filter(cards, function(id)
              return table.contains(handcards, id)
          end)
          cards = table.filter(cards, function(id)
              return not table.contains(handcards, id)
          end)
          room:swapCards(player, { { player, handcards }, { target, cards } }, policy10.name)
        end
        room:setPlayerMark(player, mark_name, 0)
        room:handleAddLoseSkills(player, "-replace_something", nil, false)
        room.logic:trigger(HG.HegLosePolicy, player, { player, policy10.name })
        local card = Fk:cloneCard("heg_replace_something")
        room:getVirtCardId(card)
        room:showVirtualCard(card, player, nil, room.logic:getCurrentEvent().id + 1)
    end,
})

return policy10
